package com.chat.datea.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.NewMusicGridViewAdapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.RecomNewsMusicEntivity;
import com.chat.datea.utils.fragment.ReplaceFragment;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

/**
 * Created by YanBinXie on 15/10/27.
 */
public class NewMusicFragment extends BaseFragment implements View.OnClickListener {
    private ImageView back;
    private GridView singer_gridView;
    private VolleySingleton singleton;
    private NewMusicGridViewAdapter gridViewAdapter;


    @Override
    protected void initData() {

        String url = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.plaza.getRecommendAlbum" + Finnal_Base.format + Finnal_Base.offset + "0" + "&limit=" + 30;
        singleton = VolleySingleton.getVolleySingleton(mActivity);
        back.setOnClickListener(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                final RecomNewsMusicEntivity entivity = gson.fromJson(response,RecomNewsMusicEntivity.class);
                gridViewAdapter = new NewMusicGridViewAdapter(mActivity,entivity);
                singer_gridView.setAdapter(gridViewAdapter);
                singer_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String album_id = entivity.getPlaze_album_list().getRM().getAlbum_list().getList().get(position).getAlbum_id();
                        AlbumInfoFragment albumInfoFragment = new AlbumInfoFragment(album_id);
                        new ReplaceFragment(albumInfoFragment, getActivity().getSupportFragmentManager(), R.id.fl_content, true, null);

                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "NewMusicFragment");
    }

    @Override
    protected void initView() {
        back = (ImageView) view.findViewById(R.id.back);
        singer_gridView = (GridView) view.findViewById(R.id.singer_gridView);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_newmusic, null);
        return view;
    }


    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }
}
